package com.walker.taogong.web;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.walker.taogong.bll.TaskBll;
import com.walker.taogong.bll.UserBll;
import com.walker.taogong.datamodel.AssignType;
import com.walker.taogong.datamodel.Task;
import com.walker.taogong.datamodel.TaskCategory;
import com.walker.taogong.datamodel.TaskHistory;
import com.walker.taogong.datamodel.TaskPay;
import com.walker.taogong.datamodel.TaskPrivacy;
import com.walker.taogong.datamodel.TaskStatus;
import com.walker.taogong.datamodel.User;

public class PostTask
{
    /** Logger. */
    private static Logger log = LoggerFactory.getLogger(PostTask.class);

    /************************ BLL 服务引用 *******************************************/

    private UserBll userBll;

    private TaskBll taskBll;

    public void setUserBll(UserBll userBll)
    {
        this.userBll = userBll;
    }

    public void setTaskBll(TaskBll taskBll)
    {
        this.taskBll = taskBll;
    }

    /************************* 启动测试用 ************************************/
    /**
     * 组件启动初期化
     */
    protected void activate(ComponentContext cc) throws Exception
    {
        log.info("{} is starting!", cc.getProperties().get("name"));
        try
        {
            Task task = new Task();
            task.setTaskTitle("tile for shoping");

            User owner = userBll.getUserByEmail("lisi@test.com");
            task.setOwner(owner.getUserName());

            TaskCategory category = new TaskCategory();
            category.setId(1);
            category.setTitle("Shopping");
            category.setDetail("this is test");
            task.setCategory(category);
            // task.setAddresses(addresses);
            task.setConfirmDeadLine("20120101");
            task.setFinishDeadLine("20120102");
            SimpleDateFormat  sd = new SimpleDateFormat("yyyyMMdd");
            task.setPostDate(sd.format(new Date(System.currentTimeMillis())));
            task.setAssignType(AssignType.AUTO);
            task.setDescription("detial description");
            task.setTaskPrivacy(TaskPrivacy.ALL);

            TaskPay taskPay = new TaskPay();
            taskPay.setParentTask(task);
            taskPay.setTaskCost(100);

            task.setTaskPay(taskPay);
            task.setStatus(TaskStatus.POSTED);
            
            TaskHistory taskHistory = new TaskHistory();
            taskHistory.setTask(task);
            taskHistory.setUser(owner);
            taskHistory.setTaskStatus(TaskStatus.POSTED);
            taskHistory.setChangedTime(sd.format(new Date(System.currentTimeMillis())));
            taskHistory.setDetail(owner.getUserName() + "Posted it");
            
            Set<TaskHistory> histories = new HashSet<TaskHistory>();
            histories.add(taskHistory);
            task.setTaskHistories(histories);
            

            taskBll.postTask(task);
            
            log.info("你已經成功添加料一個任務");
        }
        catch (Throwable exc)
        {
            log.error("Terminated with runtime exception", exc);
        }
    }

}
